package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.BannerDao;
import com.ddwl.goods.dao.BestSellingDao;
import com.ddwl.goods.mapper.BannerMapper;
import com.ddwl.goods.mapper.BestSellingMapper;
import com.ddwl.goods.model.Banner;
import com.ddwl.goods.model.BestSelling;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.BannerBo;
import com.ddwl.schema.bo.goods.BestSellingBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.BannerVo;
import com.ddwl.schema.vo.goods.BestSellingVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BestSellingService {
    @Autowired
    private BestSellingMapper bestSellingMapper;

    @Autowired
    private BestSellingDao bestSellingDao;

    public List<BestSellingVo> bannerList(Integer platform){

        //默认查商城平台（2商城 1分销商城）
        if(platform==null||platform.toString()==""){
            platform=2;
        }

        List<BestSellingVo> list=new ArrayList<>();
        for(BestSelling BestSelling:bestSellingMapper.bannerList(platform)){
            BestSellingVo bestSellingVo=new BestSellingVo();
            BeanUtils.copyProperties(BestSelling,bestSellingVo);
            list.add(bestSellingVo);
        }
        return list;
    }

    public String create(BestSellingBo bestSellingBo){
        BestSelling bestSelling=new BestSelling();
        BeanUtils.copyProperties(bestSellingBo,bestSelling);
        if(bestSellingBo.getId()!=null){
            bestSellingDao.updateByPrimaryKeySelective(bestSelling);
        }else {
            if(bestSellingBo.getPlatform()==null||bestSellingBo.getPlatform().toString()==""){
                bestSelling.setPlatform(Constant.PLATFORM_MALL);
            }
            bestSelling.setId(UUIDGenerator.getShortUUID());
            bestSelling.setBannerStatus(BaseEnum.StatusEnum.DISABLE.getCode());
            bestSellingDao.insert(bestSelling);
        }
        return bestSelling.getId();
    }

    public BestSellingVo detail(String bannerId){
        BestSellingVo bestSellingVo=new BestSellingVo();
        BestSelling bestSelling=bestSellingDao.selectByPrimaryKey(bannerId);
        BeanUtils.copyProperties(bestSelling,bestSellingVo);
        return bestSellingVo;
    }

    public PageVo<BestSellingVo> conList(PageBo<BestSellingBo> pageBo){
//        if(pageBo.getParam().getPlatform()==null||pageBo.getParam().getPlatform().toString()==""){
//            pageBo.getParam().setPlatform(Constant.PLATFORM_MALL);
//        }
        PageHelper.startPage(pageBo.getPage(),pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        PageInfo<BestSelling> pageInfo=new PageInfo<>(bestSellingMapper.findAll(pageBo.getParam()));
        List<BestSellingVo> list= BeanConverUtils.convertBeanList(pageInfo.getList(),BestSellingVo.class);
        return new PageVo<>(pageInfo.getTotal(),pageBo.getPage(),pageBo.getPageSize(),list);
    }


    public void  forbidden(BestSellingBo bestSellingBo){
        BestSelling bestSelling=new BestSelling();
        BeanUtils.copyProperties(bestSellingBo,bestSelling);
        bestSellingMapper.updateByPrimaryKey(bestSelling);
    }

    public String delete(String id){
        bestSellingDao.deleteById(id);
        return id+ Constant.HAVE_DELETED;
    }
}
